package com.zxl.controller;

import com.zxl.constants.ESConstants;
import com.zxl.feign.EsFeign;
import com.zxl.feign.GoodsFeign;
import com.zxl.vo.Goods;
import com.zxl.vo.R;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @packageName com.zxl.controller
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private EsFeign esFeign;


    @RequestMapping("/{page}.html")
    public String page(@PathVariable("page") String page, Integer id, Model model) {
        model.addAttribute("id", id);
        return "/goods/" + page;
    }

    @RequestMapping("/list")
    public String list(Model model) {
        List<Goods> list = goodsFeign.goodsList();
        model.addAttribute("goodsList", list);
        return "/goods/goodsList";
    }

    @RequestMapping("/add")
    @ResponseBody
    public R add(Goods goods) {
        Goods tgoods = goodsFeign.addGoods(goods);
        rabbitTemplate.convertAndSend(ESConstants.GOODS_EXCHANGE,"goods.add",tgoods);
        return R.success();
    }

    @RequestMapping("/savaAll")
    @ResponseBody
    public R saveAll() throws Exception {
        esFeign.save(goodsFeign.goodsList());
        return R.success();
    }


}
